<?php


namespace app\admin\common;


use fast\Date;

class WorkTime
{


    // 定义加班时间
    public $s_duty_time = 8.5; // 上班时间
    public $x_duty_time = 17.5; // 下班时间
    public $off_duty_time = 18; //计算加班时间
    public $isFreeTime = true; //是否要去除午休工作时长
    public $freeTimeMon = 12; //午休开始时间
    public $freeTimeAft = 14; //午休结束时间

    public $start_time = null;
    public $end_time = null;

    public function __construct($start_time, $end_time)
    {
        $this->start_time = $start_time;
        $this->end_time   = $end_time;
    }


    //获取加班时长
    public function work_hours()
    {
        $freeTime         = $this->freeTimeAft - $this->freeTimeMon;
        $beginArr1        = explode(" ", $this->start_time);
        $this->start_time = str_replace("-", "/", $this->start_time);
        $beginArr         = explode(" ", $this->start_time);
        $beginMonth       = intval(explode("/", $beginArr[0])[1]);
        $beginDay         = intval(explode("/", $beginArr[0])[2]);
        $beginHours       = intval(explode(":", $beginArr[1])[0]);
        $beginMin         = intval(explode(":", $beginArr[1])[1]);
        $beginHoursMin    = $beginHours + ($beginMin / 60);

        $this->end_time = str_replace("-", "/", $this->end_time);
        $end_arr        = explode(" ", $this->end_time);
        $endMonth       = intval(explode("/", $end_arr[0])[1]);
        $endDay         = intval(explode("/", $end_arr[0])[2]);
        $endHours       = intval(explode(":", $end_arr[1])[0]);
        $endMin         = intval(explode(":", $end_arr[1])[1]);
        $endHoursMin    = $endHours + ($endMin / 60);

        //如果endHoursMin时间大于上班时间都算加班班时间
        if ($endHoursMin >= $this->off_duty_time) {
            $endHoursMin = ($endHoursMin - $this->off_duty_time);
        } else {
            $endHoursMin = 0;
        }

        $daysBetween = $this->daysBetween($this->start_time, $this->end_time);

        $daysBetween = explode(',', $daysBetween);
        $date        = [];
        if (count($daysBetween) > 0) {
            $daysBetweenLen = count($daysBetween);

            if ($this->isFreeTime == true) {
                $hour = $this->x_duty_time - $this->s_duty_time - $freeTime;
                if ($daysBetweenLen == 1) {

                    $url  =
                        "https://tool.bitefu.net/jiari/?d=" . $beginArr1[0] .
                        "&info=2";
                    $data = $this->http($url, 'get');
                    //1 工作日 2调休(补节假日的) 3周未 4假日
                    if (!empty($data)) {
                        switch ($data[1]) {
                            case 1:
                            case 2:
                                $hours = $endHoursMin;
                                break;
                            case 3:
                            case 4:
                                $hours = $this->x_duty_time - $beginHoursMin;
                                if ($beginHoursMin <= $this->freeTimeMon) {
                                    $hours = $hours - $freeTime;
                                }
                                $hours = ($hours + $endHoursMin);
                                break;
                            default:
                                $hours = 0;
                                break;
                        }
                    }
                } else {
                    if ($daysBetweenLen == 2) {
                        //跨一天
                        //第一天的时长
                        $hours = $this->x_duty_time - $beginHoursMin;

                        //是否有午休
                        if ($beginHoursMin <= $this->freeTimeMon) {
                            $hours = $hours - $freeTime;
                        }

                        //第二天的时长
                        $hours += $endHoursMin - $this->s_duty_time;

                        //是否有午休
                        if ($endHoursMin >= $this->freeTimeAft) {
                            $hours = $hours - $freeTime;
                        }

                    } else {
                        //跨两天以上
                        //第一天的时长
                        $hours = $this->x_duty_time - $beginHoursMin;
                        //是否有午休
                        if ($beginHoursMin <= $this->freeTimeMon) {
                            $hours = $hours - $freeTime;
                        }

                        //中间时长
                        $hours += ($daysBetweenLen - 2) * ($hour);

                        //最后一天时长
                        $hours += $endHoursMin - $this->s_duty_time;

                        //是否有午休
                        if ($endHoursMin >= $this->freeTimeAft) {
                            $hours = $hours - $freeTime;
                        }
                    }
                }
                $days = floor($hours / $hour);
                $date = [
                    "days"  => $days,
                    "hours" => $hours,
                ];
            } else {
                //非午休
                $hour = $this->x_duty_time - $this->s_duty_time;

                if ($daysBetweenLen == 1) {
                    //同一天
                    $hours = ($endHoursMin) - ($beginHoursMin);
                } else {
                    if ($daysBetweenLen == 2) {
                        //跨一天
                        $hours = $this->x_duty_time - $beginHoursMin;
                        //第二天的时长

                        $hours += $endHoursMin - $this->s_duty_time;
                    } else {
                        //跨两天以上
                        //第一天的时长
                        $hours = $this->x_duty_time - $beginHoursMin;
                        //中间时长
                        $hours += ($daysBetweenLen - 2) * ($hour);
                        //最后一天时长
                        $hours += $endHoursMin - $this->s_duty_time;
                    }
                }
                $days = floor($hours / $hour);
//                if ($days > 0){
//                    $time = $days * ($this->x_duty_time - $this->s_duty_time - $freeTime);
//                }else{
//                    $time = $days;
//                }
                $date = [
                    "days"  => $days,
                    "hours" => $hours,
                ];
            }
        }
        return $date;
    }

    public function daysBetween($sDate1, $sDate2)
    {

        $arr      = "";
        $sDate1   = substr($sDate1, 0, 10);
        $sDate2   = substr($sDate2, 0, 10);
        $dt_start = strtotime($sDate1);
        $dt_end   = strtotime($sDate2);
        while ($dt_start <= $dt_end) {
            $arr      .= date('Y/m/d', $dt_start) . ",";
            $dt_start = strtotime('+1 day', $dt_start);
        }
        return rtrim($arr, ',');
    }

    public function http($url, $method, $postfields = null, $headers = [])
    {
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
        switch ($method) {
            case 'POST':
                curl_setopt($ci, CURLOPT_POST, true);
                if (!empty($postfields)) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                    $this->postdata = $postfields;
                }
                break;
        }
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, true);

        $response  = curl_exec($ci);
        $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

        curl_close($ci);
        return [$http_code, $response];
    }
}
